package hu.imind.phonecalc.server;

import java.io.IOException;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gdata.client.http.AuthSubUtil;

@SuppressWarnings("serial")
public class LoginCallBackServlet extends HttpServlet {
    private static final Logger LOG = Logger.getLogger(ContactServiceImpl.class.getName());

    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException {
        String token = AuthSubUtil.getTokenFromReply(req.getQueryString());
        if (token != null) {
            // authSub authorization here
            try {
                String sessionToken = AuthSubUtil.exchangeForSessionToken(token, null);
                Cookie cookie = new Cookie("googleAuthToken", sessionToken);
                res.addCookie(cookie);
            } catch (Exception e) {
                LOG.warning("EXCEPTION LoginCallBack: " + e.getMessage());
            }
            res.sendRedirect("/");
        }
    }
}
